/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.odg.m_ui;

import java.awt.Color;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import org.opu.odg.dg.incomingdata.ContextIteration;
import org.opu.odg.m_ui.beans.AbstractBean;
import org.opu.odg.m_ui.beans.ContentBean;
import org.opu.odg.m_ui.beans.VariableBean;

/**
 *
 * @author yura
 */
public class TreeCellRendererImpl extends DefaultTreeCellRenderer {

    private static final ImageIcon VAR_ICON = new ImageIcon(
            TreeCellRendererImpl.class.getResource("dolar-16.png"));
    private static final ImageIcon CONTENT_ICON = new ImageIcon(
            TreeCellRendererImpl.class.getResource("grille-16.png"));

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean selected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        
        Component component = super.getTreeCellRendererComponent(tree, value,
                selected, expanded, leaf, row, hasFocus);

        if (value instanceof DefaultMutableTreeNode) {
            Object object = ((DefaultMutableTreeNode) value).getUserObject();
            if (object != null) {
                if (object instanceof AbstractBean) {
                    if (!((AbstractBean) object).isConfigured()) {
                        setForeground(Color.RED);
                    } else if (object instanceof VariableBean
                            && ((VariableBean)object).getCurrentContent() != null
                            && ContextIteration.ITERATION.equals(
                                ((VariableBean)object).getName())) {
                        setForeground(Color.BLUE);
                    }
                }
                setIcon(null);
                if (object instanceof ContentBean) {
                    setIcon(CONTENT_ICON);
                }
                if (object instanceof VariableBean) {
                    setIcon(VAR_ICON);
                }
            }
        }

        return component;
    }
}
